﻿
using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace LaunchPad.Core
{
    public abstract class Track : INotifyPropertyChanged, IComparable
    {
        protected Track()
        {
            Timeline = new TrackTimeline();
        }

        private TrackTimeline _timeline;
        private Field _field;
        private bool _selected;

        [XmlArrayItem(typeof(TrackEvent))]
        [XmlArrayItem(typeof(EffectTrackEvent))]
        public TrackTimeline Timeline
        {
            get { return _timeline; }

            set
            {
                if (_timeline != value)
                {
                    _timeline = value;
                    OnPropertyChanged("Timeline");
                }
            }
        }

        [XmlIgnore]
        public bool IsSelected
        {
            get { return _selected; }
            set
            {
                if (_selected != value)
                {
                    _selected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }

        [XmlIgnore]
        public Field Field
        {
            get { return _field; }

            set
            {
                if (_field != value)
                {
                    _field = value;
                    OnPropertyChanged("Field");
                }
            }
        }

        public bool TryAddEvent(TrackEvent e)
        {
            return Timeline.TryAddEvent(e);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        public int CompareTo(object obj)
        {
            var track = (Track) obj;

            if (track is TimeTrack)
                return -1;

            return Timeline.Start.CompareTo(track.Timeline.Start);
        }
    }
}
